package com.kuyun.base.controller;

import java.awt.Container;
import java.util.Dictionary;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.codehaus.jackson.map.deser.ValueInstantiators.Base;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kuyun.base.base.BaseController;
import com.kuyun.base.base.Page;
import com.kuyun.base.model.BaseDictionary;
import com.kuyun.base.model.BasePrivilege;
import com.kuyun.base.model.HttpResult;
import com.kuyun.base.model.ValidFormResult;
import com.kuyun.base.model.ZtreeBean;
import com.kuyun.base.util.Constants;

/**
 * 字典管理controller
 * 
 * @author 王坤
 *
 * @date 2016年6月19日下午00:28:37
 *
 */
@Controller
@RequestMapping("/webback/BaseDictionary")
public class BaseDictionaryController extends BaseController {

	/**
	 * 获取字典树信息
	 */
	@RequiresPermissions("DICTIONARY_LIST")
	@RequestMapping("/getDictionaryTree.do")
	@ResponseBody
	public HttpResult getDictionaryTree() {
		HttpResult result = new HttpResult();
		try {
			List<ZtreeBean> list = baseDictionaryService.getDictionaryTree();
			result.setObj(list);
			result.setResultCode(Constants.RESULT_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setObj(null);
			result.setResultCode(Constants.RESULT_FINAL);
		}
		return result;
	}

	/**
	 * 跳转字典列表
	 */
	@RequiresPermissions("DICTIONARY_LIST")
	@RequestMapping("/toListDictionary.do")
	public String toListDictionary() {
		return "dictionary/dictionary_list";
	}

	/**
	 * 获取父节点下字典信息
	 */
	@RequiresPermissions("DICTIONARY_LIST")
	@ResponseBody
	@RequestMapping("/getChildrenDictionary.do")
	public Page<BaseDictionary> getChildrenDictionary(Page<BaseDictionary> page,String parentId) {
		page = baseDictionaryService.findByParentCode(page, parentId, null);
		return page;
	}
	
	/**
	 * 跳转到新增页面
	 * 
	 * @author 王坤
	 *
	 * @date 2016年6月18日下午7:27:37
	 *
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("DICTIONARY_ADD")
	@RequestMapping("/toAddDictionary.do")
	public String toAddDictionary(String parentId, String level, Model model) throws Exception { // 父亲的id
		BaseDictionary base = null; // 父权限
		BaseDictionary typeDic = null;
		if("root".equals(parentId)||"0".equals(level)){
			base=new BaseDictionary();
			base.setBaseDicCode("0");
			base.setBaseDicName("系统字典");
			
		}else{
			base=baseDictionaryService.findByID(parentId);
		}
		model.addAttribute("base", base);
		return "dictionary/dictionary_edit";

	}
	
	/**
	 * 跳转到修改页面
	 * 
	 * @author 王坤
	 *
	 * @date 2016年6月18日下午7:27:37
	 *
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("DICTIONARY_UPDATE")
	@RequestMapping("/toUpdateDictionary.do")
	public String toUpdateDictionary(String id,String parentId, Model model) throws Exception { // 父亲的id
		BaseDictionary base = null; // 父权限
		BaseDictionary baseDictionary = null; // 父权限
		if("root".equals(parentId)){
			base=new BaseDictionary();
			base.setBaseDicCode("0");
			base.setBaseDicName("系统字典");
		}else{
			base=baseDictionaryService.findByID(parentId);
		}
		baseDictionary=baseDictionaryService.findByID(id);
		model.addAttribute("base", base);
		model.addAttribute("baseDictionary", baseDictionary);
		return "dictionary/dictionary_edit";
	}

	/**
	 * 新增字典对象
	 */
	@RequiresPermissions("DICTIONARY_ADD")
	@RequestMapping(value="/addDictionary.do",method=RequestMethod.POST)
	@ResponseBody
	public ValidFormResult addDictionary( BaseDictionary baseDictionary) {
		String flag = baseDictionaryService.insert(baseDictionary);
		ValidFormResult result = new ValidFormResult();
		if("n".equals(flag)){
			result.setInfo("操作有问题！大兄弟");
			result.setStatus(ValidFormResult.N);
		}else{
			result.setStatus(ValidFormResult.Y);
		}
		return result;
	}

	/**
	 * 更新字典对象
	 */
	@RequiresPermissions("DICTIONARY_UPDATE")
	@RequestMapping("/updateDictionary.do")
	@ResponseBody
	public ValidFormResult updateDictionary( BaseDictionary baseDictionary) {
		String flag = baseDictionaryService.update(baseDictionary);
		ValidFormResult result = new ValidFormResult();
		if("n".equals(flag)){
			result.setInfo("操作有问题！大兄弟");
		}
		result.setStatus(flag);
		return result;
	}
	
	/**
	 * 删除字典信息
	 */
	@RequiresPermissions("DICTIONARY_UPDATE")
	@RequestMapping("/deleteDictionary.do")
	@ResponseBody
	public ValidFormResult deleteDictionary(String id) {
		ValidFormResult result=baseDictionaryService.deleteDic(id);
		return result;
	}
	
	/**
	 * 判断code是否重复
	 * */
	@RequestMapping("/checkDictionaryCode.do")
	@ResponseBody
	public ValidFormResult checkDictionaryCode(String param, String id) {
		ValidFormResult result = new ValidFormResult();
		BaseDictionary base = new BaseDictionary();
		base.setBaseDicCode(param);
		try {
			if (baseDictionaryService.checkCode(base,id)) {
				result.setStatus(ValidFormResult.Y);
			} else {
				result.setInfo("该编码已经存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setInfo("系统错误，请稍后再试");
		}
		return result;
	}
}
